package com.source.baseData.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineStatus.vo.CenterVo;
import com.source.baseData.entity.SlaughterHouse;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.source.baseData.vo.SlaughterHouseAddressVo;
import com.source.baseData.vo.SlaughterHouseMapVo;
import com.source.labelAndBarcode.vo.SlaughterHouseVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author Joey
 * @since 2021-02-10
 */
public interface SlaughterHouseMapper extends BaseMapper<SlaughterHouse> {

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode ,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechat,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile\n" +
            "FROM\n" +
            "\tslaughter_house sh , \n" +
            "\tsys_user su\n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 and su.userId = sh.userId;")
    List<SlaughterHouseVo> findAllVo();

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND sh.`status` = 1;")
    List<SlaughterHouseVo> findAllOnPermission();

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND sh.`status` = 2;")
    List<SlaughterHouseVo> findAllOnPermissionAudit();

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND sh.`status` = 3;")
    List<SlaughterHouseVo> findAllOnDeletionAudit();

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND slaughterHouseId = #{id};")
    SlaughterHouseVo findVoById(String id);

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND sh.`status` = 1")
    IPage<SlaughterHouseVo> findAllVoOnPaginationAndPermission(Page<SlaughterHouseVo> page);

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND sh.`status` = 1 AND CONCAT(slaughterHouseName,district,address, su.`name`) LIKE #{keyWord}")
    IPage<SlaughterHouseVo> findAlikeOnPAP(Page<SlaughterHouseVo> page, String keyWord);

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND sh.`status` = 1 AND slaughterHouseName LIKE #{keyWord}")
    IPage<SlaughterHouseVo> findVoByNameAndP(Page<SlaughterHouseVo> page, String keyWord);

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId AND sh.`status` = #{status}")
    IPage<SlaughterHouseVo> findAllOnMultiStatus(Page<SlaughterHouseVo> page, int status);

    @Select(value = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId")
    IPage<SlaughterHouseVo> findAllOnPagination(Page<SlaughterHouseVo> page);

    String SELECT = "SELECT\n" +
            "\tslaughterHouseId,\n" +
            "\tslaughterhousename,\n" +
            "\tlatitude,\n" +
            "\tlongitude,\n" +
            "\taddress,\n" +
            "\tprovince,\n" +
            "\tcity,\n" +
            "\tsh.userId,\n" +
            "\tcontacts,\n" +
            "\tdistrict,\n" +
            "\testablishDate,\n" +
            "\tmarketIntroduction,\n" +
            "\tmarketPictures,\n" +
            "\tsh.createDate,\n" +
            "\tsh.`status`,\n" +
            "\tsh.isDelete,\n" +
            "\tslaughterHouseCode,\n" +
            "\tsu.`name`,\n" +
            "\tsu.qq,\n" +
            "\tsu.wechatCode,\n" +
            "\tsu.wechat,\n" +
            "\tsu.phone,\n" +
            "\tsu.mobile \n" +
            "FROM\n" +
            "\tslaughter_house sh,\n" +
            "\tsys_user su \n" +
            "WHERE\n" +
            "\tsh.isDelete = 0 \n" +
            "\tAND su.userId = sh.userId ";

    @Select(SELECT + " AND (sh.`status` = 2 or sh.`status` = 3 ) ")
    IPage<SlaughterHouseVo> findByUnChecked(Page<SlaughterHouseVo> page);

    @Select(SELECT + "AND sh.`status` = 1 AND sh.`district` Like #{name}")
    List<SlaughterHouseVo> findByDistrict(String name);

    @Select(SELECT + " AND (sh.`status` = 2 or sh.`status` = 3 )  " +
            " AND CONCAT_WS(',',slaughterHouseName, district, su.`name`, address ) Like #{keyWord} ")
    IPage<SlaughterHouseVo> findByUnCheckedAndKeyWord(String keyWord, Page<SlaughterHouseVo> page);

    /**
     * 查询所有屠宰场的id和名称
     * @return
     */
    @Select("select slaughterHouseId,slaughterHouseName from slaughter_house\n" +
            "where isDelete=0")
    List<SlaughterHouseMapVo> selectAllSlaughterHouseMapVo();

    /**
     * selectSlaughterHouseAddressVo
     * @return
     */
    @Results(value = {
            @Result(property ="slaughterHouseId",column = "slaughterHouseId"),
            @Result(property = "slaughterHouseName",column = "slaughterHouseName"),
            @Result(property = "centerVo",column ="slaughterHouseId" ,javaType = CenterVo.class,one =@One(select = ("com.source.baseData.mapper.SlaughterHouseMapper.selectSlaughterHouseAddressVoById")))
    })
    @Select("select sh.slaughterHouseId,sh.slaughterHouseName \n" +
            "from slaughter_house as sh\n" +
            "where sh.isDelete=0\n")
    List<SlaughterHouseAddressVo> selectSlaughterHouseAddressVo();

    /**
     * selectSlaughterHouseAddressVoById
     * @param slaughterHouseId
     * @return
     */
    @Select("select sh.longitude as lng,sh.latitude as lat from slaughter_house as sh\n" +
            "where sh.isDelete=0\n" +
            "and sh.slaughterHouseId='${slaughterHouseId}' ")
    CenterVo selectSlaughterHouseAddressVoById(@Param("slaughterHouseId") String slaughterHouseId);
}
